import request from '@/utils/request';
import { API_PATH_PRE } from '@/generic/ConfigParams';

const GROUP = "group";
const VDISK = 'vdisk';

export async function queryGroupUsers(params = {}, parse = false) {
    const gid = params.id || params.name;
    const api_url = `${API_PATH_PRE}user?group=${gid}`;
    console.log("===>> queryGroupUsers", api_url, params, gid)
    return request(api_url, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem("token")}`,
        },
        parseResponse: parse,
    })
}
export async function updateGroupUsers(params = {}, parse = false) {
    
    if (params.id) {
        const api_url = `${API_PATH_PRE}${GROUP}/${params.id}`;
        console.log("===>> updateGroupUsers", api_url)
        const { direction, moveKeys } = params.data;
        const methods = direction == 'right' ? "addusers" : "removeusers";
        const _data = {
            "methods": methods,
            "uids": moveKeys
        }
        // console.log("===>> POST", _data)
        return request(api_url, {
            method: "POST",
            headers: {
                'Authorization': `Bearer ${localStorage.getItem("token")}`,
                'Content-Type': "application/json; charset=UTF-8",
            },
            data: JSON.stringify(_data),  // array to json
            parseResponse: parse,
        })
    }

    return false;
}

export async function queryGroups(params = {}, parse = false) {
    const gid = params.id || params.name;
    const api_url = `${API_PATH_PRE}${GROUP}${ gid ? `/${encodeURI(gid)}` : '' }`;
    console.log("===>> queryGroups", api_url, params, gid)
    return request(api_url, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem("token")}`,
        },
        parseResponse: parse,
    })
}
export async function addGroup(params = {}, parse = false) {

    const api_url = `${API_PATH_PRE}${GROUP}`;
    console.log("===>> addGroup", api_url, params)
    if (params.name) {

        return request(api_url, {
            method: "POST",
            headers: {
                'Authorization': `Bearer ${localStorage.getItem("token")}`,
            },
            data: {
                'name': params.name,
                'desc': params.desc || '',
            },
            parseResponse: parse,
        });
    }

    return false;
}
export async function deleteGroup(params = {}, parse = false) {
    // 批量用户删除不知道做没做？
    if (params.id) {
        const gid = encodeURI(params.id);
        const api_url = `${API_PATH_PRE}${GROUP}/${gid}`;
        console.log("===>> deleteGroup", api_url)
        return request(api_url, {
            method: "DELETE",
            headers: {
                'Authorization': `Bearer ${localStorage.getItem("token")}`,
            },
            parseResponse: parse,
        })
    }

    return false;
}
export async function updateGroup(params = {}, parse = false) {
    // 批量用户删除不知道做没做？
    if (params.id) {
        const gid = encodeURI(params.id);
        const api_url = `${API_PATH_PRE}${GROUP}/${gid}`;
        console.log("===>> updateGroup", api_url)
        console.log("===>> patch", params.patch, params.id)
        for (const itemKey in params.patch) {
            // 'extra_'代表执行特定处理方法（handMethod）
            if (itemKey.indexOf('_') > -1 && itemKey.split('_')[0] == 'extra') {
                const response = params.patch[itemKey].handMethod({ id: gid, data: params.patch[itemKey].values });
                delete params.patch[itemKey];
                // console.log("==>> delete", params.patch)
                return response;
            }
        }
        if (Object.keys(params.patch).length)
        return request(api_url, {
            method: "PATCH",
            headers: {
                'Authorization': `Bearer ${localStorage.getItem("token")}`,
                'Content-Type': "application/json; charset=UTF-8",
            },
            data: { ...params.patch },
            parseResponse: parse,
        })
    }

    return false;
}

export async function queryDisks(params = {}, parse = false) {
    const vid = params.id || params.name;
    const api_url = `${API_PATH_PRE}${VDISK}${vid ? `/${encodeURI(vid)}` : ''}`;
    console.log("===>> queryDisks", api_url, params, vid)
    return request(api_url, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem("token")}`,
        },
        parseResponse: parse,
    })
}
export async function addDisk(params = {}, parse = false) {

    if (params.id) {
        const api_url = `${API_PATH_PRE}${VDISK}`;
        console.log("===>> addDisk", api_url, params)
        return request(api_url, {
            method: "POST",
            headers: {
                'Authorization': `Bearer ${localStorage.getItem("token")}`,
            },
            data: {
                'ID': params.id,
                'name': params.name || '',
                'desc': params.desc || '',
                'capacity': params.capacity || -1,
                'used': params.used || 0,
                'enable': params.enable || true,
                'cl': params.cl || 1,
                'perm': params.perm || 0,
            },
            parseResponse: parse,
        });
    }

    return false;
}
export async function deleteDisk(params = {}, parse = false) {
    // 批量用户删除不知道做没做？
    if (params.id) {
        const vid = encodeURI(params.id);
        const api_url = `${API_PATH_PRE}${VDISK}/${vid}`;
        console.log("===>> deleteDisk", api_url)
        return request(api_url, {
            method: "DELETE",
            headers: {
                'Authorization': `Bearer ${localStorage.getItem("token")}`,
            },
            parseResponse: parse,
        })
    }

    return false;
}
export async function updateDisk(params = {}, parse = false) {
    
    if (params.id) {
        const vid = encodeURI(params.id.toString().replace('\\', '/'));
        const api_url = `${API_PATH_PRE}${VDISK}/${vid}`;
        console.log("===>> updateDisk", api_url)
        console.log("===>> patch", params.patch, params.id, vid)
        return request(api_url, {
            method: "PATCH",
            headers: {
                'Authorization': `Bearer ${localStorage.getItem("token")}`,
                'Content-Type': "application/json; charset=UTF-8",
            },
            data: { ...params.patch },
            parseResponse: parse,
        })
    }

    return false;
}

const AUDIT = 'audit';
export async function queryUserAudit(params = {}, parse = false) {
    const uid = params.id || params.name;
    const api_url = `${API_PATH_PRE}${AUDIT}?user=${uid}`
    console.log("===>> queryDisks", api_url, params, vid)
    return request(api_url, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem("token")}`,
        },
        parseResponse: parse,
    })
}